﻿using LightingPlatform.TCP;
using LightingPlatform.Tools;
using LightingPlatform.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace LightingPlatform.View.UserManager
{
    public partial class UserWindow : ChildWindow
    {
        private UserVM _sourceUser;

        public UserWindow()
        {
            InitializeComponent();
            this.OKButton.Click += OnAddButton_Click;
            this.UserRole.SelectedIndex = 0;
        }

        public User User
        {
            get;
            private set;
        }

        private async void OnAddButton_Click(object sender, RoutedEventArgs e)
        {
            if (!Validate())
            {
                return;
            }

            User.Builder builder = new User.Builder();
            builder.SetName(this.UserName.Text);
            builder.SetPassword(this.UserPassword1.Password);
            builder.SetRoleType(this.UserRole.SelectedIndex + 1);

            this.OKButton.IsEnabled = false;
            WebServiceProxy proxy = new WebServiceProxy();
            int result = await proxy.AddUser(builder.Build().ToByteArray());
            builder.SetId(result);
            User = builder.Build();
            this.DialogResult = true;
        }

        public UserWindow(UserVM user, bool isEdited)
        {
            InitializeComponent();

            _sourceUser = user;
            if (isEdited)
            {
                this.OKButton.Click += OnEditButton_Click;
            }
            else
            {
                this.OKButton.Visibility = Visibility.Collapsed;
                this.CancelButton.Content = "关闭";

                this.UserName.IsReadOnly = true;
                this.UserPassword1.IsEnabled = false;
                this.UserPassword2.IsEnabled = false;
                this.UserRole.IsEnabled = false;

                this.UserName.Text = _sourceUser.Name;
                this.UserPassword1.Password =
                this.UserPassword2.Password = _sourceUser.Password;
                this.UserRole.SelectedIndex = (int)_sourceUser.RoleType - 1;
            }
        }

        private async void OnEditButton_Click(object sender, RoutedEventArgs e)
        {
            if (!Validate())
            {
                return;
            }

            bool hasChanged = false;
            User.Builder builder = new User.Builder();
            if (_sourceUser.Name != this.UserName.Text)
            {
                hasChanged = true;
                builder.SetName(this.UserName.Text);
            }

            if (_sourceUser.Password != this.UserPassword1.Password)
            {
                hasChanged = true;
                builder.SetName(this.UserName.Text);
            }

            int roleType = this.UserRole.SelectedIndex + 1;
            if (_sourceUser.RoleType != roleType)
            {
                hasChanged = true;
                builder.SetRoleType(roleType);
            }

            if (hasChanged)
            {
                this.OKButton.IsEnabled = false;
                builder.SetId(_sourceUser.ID);
                WebServiceProxy proxy = new WebServiceProxy();
                bool result = await proxy.UpdateUser(builder.Build().ToByteArray());

                this.OKButton.IsEnabled = true;
                if (result)
                {
                    _sourceUser.Name = this.UserName.Text;
                    _sourceUser.Password = this.UserPassword1.Password;
                    _sourceUser.RoleType = roleType;
                }
                else
                {
                    MessageHelper.ShowError("更新用户信息出错。");
                    return;
                }
            }

            this.DialogResult = true;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private bool Validate()
        {
            if (string.IsNullOrEmpty(this.UserName.Text))
            {
                MessageHelper.ShowMessage("用户名不能为空。");
                this.UserName.Focus();
                return false;
            }

            if (string.IsNullOrEmpty(this.UserPassword1.Password))
            {
                MessageBox.Show("密码不能为空。");
                this.UserPassword1.Focus();
                return false;
            }

            if (string.IsNullOrEmpty(this.UserPassword2.Password))
            {
                MessageBox.Show("密码不能为空。");
                this.UserPassword2.Focus();
                return false;
            }

            if(this.UserPassword1.Password != this.UserPassword2.Password)
            {
                MessageBox.Show("两次输入的密码不一致。");
                this.UserPassword1.Focus();
                return false;
            }

            return true;
        }
    }
}

